package com.groups.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.xml.ws.WebServiceException;

import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;

import com.groups.common.SPageBean;
import com.groups.entity.Good;
import com.groups.service.AllGoodService;

public class AllGoodAction extends BaseAction{
	private AllGoodService allGoodService;
	private List<Good> goods;
	private Good good;
	private int id;
	private Object[]  goodInfo;
	private boolean flag;
	
	public Object[] getGoodInfo() {
		return goodInfo;
	}

	public void setGoodInfo(Object[] goodInfo) {
		this.goodInfo = goodInfo;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Good getGood() {
		return good;
	}

	public void setGood(Good good) {
		this.good = good;
	}

	public List<Good> getGoods() {
		return goods;
	}

	public void setGoods(List<Good> goods) {
		this.goods = goods;
	}

	public AllGoodService getAllGoodService() {
		return allGoodService;
	}

	public void setAllGoodService(AllGoodService allGoodService) {
		this.allGoodService = allGoodService;
	}

	public String toAllGood() throws Exception{
		return "all";
	}
	public String listAllAction() throws Exception{
		int offset = 0;
		if(request.getParameter("pager.offset")==null){
			offset = 0;
		}
		else{
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		SPageBean spb = new SPageBean();
		System.out.println(offset);
		spb.setOffset(offset);
		spb.setPageSize(4);
		goods  = allGoodService.getAllGoodsByPage(spb);
		request.setAttribute("SPB", spb);
		return "all";
	}
	
	public String toGoodList() throws Exception{
		Good g = allGoodService.checkGood(good);
		int offset = 0;
		if(request.getParameter("pager.offset")==null){
			offset = 0;
		}
		else{
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		SPageBean spb = new SPageBean();
		spb.setOffset(offset);
		spb.setPageSize(4);
		goods  = allGoodService.getAllGoodsByPage(spb);
		request.setAttribute("SPB", spb);
		request.setAttribute("good",g);
		return "list";
	}
	public String toAdd() throws Exception{
		int[] data1 = {1,2,3,4,5,6,7,8,9};
		Random r = new  Random();
		StringBuffer str1= new StringBuffer();
		StringBuffer str2= new StringBuffer();
		for(int i=0;i<5;i++){
			str1.append(data1[r.nextInt(9)]);
        }
		str2.append(data1[r.nextInt(9)]);
		str1.append(str2);
		String str = str1.toString();
		id = Integer.parseInt(str);
		return "add";
	}
	public String doAdd() throws Exception{	
		String goodUrl  = good.getGoodUrl();
		int a = goodUrl.lastIndexOf("image");
		goodUrl = goodUrl.substring(a);	
		good.setGoodUrl(goodUrl);
		if(allGoodService.addGood(good)){
			response.sendRedirect("allGood!toGoodList.action");
			return null;
		}
		return "add";
	}
	public String getGoodDetail(){
		int goodId = Integer.parseInt(request.getParameter("goodId"));
		goodInfo = allGoodService.getGoodDetail(goodId);
		Date now = new Date();
		Date endTime = (Date) goodInfo[6];
		if(endTime.after(now)){
			flag = false;
		}else{
			flag = true;
		}
		request.setAttribute("GoodInfo", goodInfo);
		return "detail";
	}
	
	public String toEdit() throws Exception{
		good = allGoodService.getGoodById(good.getGoodId());
		request.setAttribute("good",good);
		return "edit";
	}
	
	public String doEdit() throws Exception{
		String goodUrl  = good.getGoodUrl();
		int a = goodUrl.lastIndexOf("image");
		goodUrl = goodUrl.substring(a);	
		good.setGoodUrl(goodUrl);
		if(allGoodService.updateGood(good)){
			response.sendRedirect("allGood!toGoodList.action");
			return null;
		}
		return "edit";
	}
	
	public String toDetail() throws Exception{
		good = allGoodService.getGoodById(good.getGoodId());
		request.setAttribute("good",good);
		return "GoodDetail";
	}
	
	public String doDelete() throws Exception{
		response.setContentType("text/xml; charset=utf-8"); 
		PrintWriter out = response.getWriter();
		if(allGoodService.deleteGood(good.getGoodId())){
			out.println("<result>ok</result>");
		}
		else{
			out.println("<result>xx</result>");
		}
		return null;
	}
	
	public String toQuery() throws Exception{
		int offset = 0;
		if(request.getParameter("pager.offset")==null){
			offset = 0;
		}
		else{
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		SPageBean spb = new SPageBean();
		System.out.println(offset);
		spb.setOffset(offset);
		spb.setPageSize(4);
		goods = allGoodService.QueryGood(spb, good);
		request.setAttribute("SPB", spb);	
		return "list";
	}
}
 